package com.gangye.controller;

import com.gangye.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Classname StudentHandler
 * @Description TODO
 * @Date 2019/12/26 19:29
 * @Created by gangye
 * @Email qigangye@jsaepay.com
 */
@RestController
public class StudentHandler {

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/set")
    public void set(@RequestBody Student student){
        redisTemplate.opsForValue().set("student",student);
    }

    @GetMapping("/get/{key}")
    public Student get(@PathVariable("key") String key){
        return (Student) redisTemplate.opsForValue().get(key);
    }

    @DeleteMapping("/delete/{key}")
    public boolean delete(@PathVariable("key") String key){
        redisTemplate.delete(key);
        return redisTemplate.hasKey(key);
    }

    @GetMapping("/stringType")
    public String stringTypeTest(){
        redisTemplate.opsForValue().set("str","helloWorld");
        String s = (String) redisTemplate.opsForValue().get("str");
        return s;
    }

    @GetMapping("/listType")
    public List<String> listTypeTest(){
        ListOperations<String,String> listOperations = redisTemplate.opsForList();
        listOperations.leftPush("list","hello");
        listOperations.leftPush("list","world");
        listOperations.leftPush("list","java");
        List<String> list =  listOperations.range("list",0,2);
        return list;
    }

    @GetMapping("/setType")
    public Set<String> setTypeTest(){
        SetOperations<String,String> setOperations = redisTemplate.opsForSet();
        setOperations.add("set","hello");
        setOperations.add("set","hello");
        setOperations.add("set","world");
        setOperations.add("set","world");
        setOperations.add("set","java");
        Set<String> set = setOperations.members("set");
        return set;
    }

    @GetMapping("/zsetType")
    public Set<String> zsetTypeTest(){
        ZSetOperations<String,String> zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("zset","hello",1);
        zSetOperations.add("zset","world",2);
        zSetOperations.add("zset","java",3);
        Set<String> set = zSetOperations.range("set",0,2);
        return set;
    }

    @GetMapping("/hashType")
    public void hashTypeTest(){
        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
        hashOperations.put("key","hashKey","hello");
        System.out.println(hashOperations.get("key","hashkey"));
    }
}
